﻿using AutoMapper;
using B.S.Packaged.Domain;
using B.S.Packaged.ErrorCode;
using B.S.Packaged.WebAPI.Read.Application.Command.CompletedfinishedCommand;
using MediatR;
using SqlSugar;

namespace B.S.Packaged.WebAPI.Read.Application.CommandHander.CompletedfinishedHandler
{
    public class GetCompletedfinishedHandler : IRequestHandler<GetCompletedfinishedCommand, APIResult<APIPaging<Completedfinished>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public GetCompletedfinishedHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<APIResult<APIPaging<Completedfinished>>> Handle(GetCompletedfinishedCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<Completedfinished>> reat = new APIResult<APIPaging<Completedfinished>>();
            var listtt = await db.Queryable<Completedfinished>().Where(x => !x.IsDel).ToListAsync();

            if (!string.IsNullOrEmpty(request.product_name))
            {
                listtt = listtt.Where(x => x.product_name.Contains(request.product_name)).ToList();
            }
            if (!string.IsNullOrEmpty(request.packaging_method))
            {
                listtt = listtt.Where(x => x.packaging_method.Contains(request.packaging_method)).ToList();
            }
            if (!string.IsNullOrEmpty(request.production_workshop))
            {
                listtt = listtt.Where(x => x.production_workshop.Contains(request.production_workshop)).ToList();
            }
            if (!string.IsNullOrEmpty(request.production_line))
            {
                listtt = listtt.Where(x => x.production_line.Contains(request.production_line)).ToList();
            }
            if (!string.IsNullOrEmpty(request.inspection_status))
            {
                listtt = listtt.Where(x => x.inspection_status.Contains(request.inspection_status)).ToList();
            }
            if (!string.IsNullOrEmpty(request.product_label))
            {
                listtt = listtt.Where(x => x.product_label.Contains(request.product_label)).ToList();
            }
            if (!string.IsNullOrEmpty(request.barcode))
            {
                listtt = listtt.Where(x => x.barcode.Contains(request.barcode)).ToList();
            }
            if (!string.IsNullOrEmpty(request.packaging_staff))
            {
                listtt = listtt.Where(x => x.packaging_staff.Contains(request.packaging_staff)).ToList();
            }
            if (!string.IsNullOrEmpty(request.inspection_staff))
            {
                listtt = listtt.Where(x => x.inspection_staff.Contains(request.management_staff)).ToList();
            }
            if (request.production_date != null)
            {
                listtt = listtt.Where(x => x.production_date.Date == request.production_date.Value.Date).ToList();
            }

            int totalCount = listtt.Count();
            int PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            listtt = listtt.OrderByDescending(a => a.id).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize).ToList();
            APIPaging<Completedfinished> paging = new APIPaging<Completedfinished>()
            {
                TotalCount = totalCount,
                TotalPage = PageCount,
                list = listtt.ToList(),

            };

            reat.code = APIResultCode.success;
            reat.msg = "包装成品完工表显示成功";
            reat.data = paging;
            return reat;
        }
    }
}
